package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.ArrayList;
import java.util.List;

public class EveryDayNoticeEventAdapter extends BaseAdapter {
    private List<YNAlbum> datas;
    private Context context;
    private LayoutInflater inflater;
    private List<Integer> select = new ArrayList<>();

    public EveryDayNoticeEventAdapter(Context context, List<YNAlbum> datas) {
        this.context = context;
        this.datas = datas;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        YNAlbum ynAlbum = datas.get(position);
        ViewHold viewHold = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.yn_item_syncupload,
                    null);
            viewHold = new ViewHold();
            viewHold.item = (LinearLayout) convertView
                    .findViewById(R.id.sync_item);
            viewHold.name = (TextView) convertView
                    .findViewById(R.id.sync_item_name);
            viewHold.pic = (SimpleDraweeView) convertView
                    .findViewById(R.id.sync_item_pic);
            viewHold.ok = (ImageView) convertView
                    .findViewById(R.id.sync_item_ok);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        ImageOptHelper.showFrescoHome(Uri.parse(YNUrlHelper.getHomeImgUrl(context, ynAlbum.getGpic())), viewHold.pic, context);
        viewHold.name.setText(ynAlbum.getGname());
        final YNAlbum ynAlbum2 = ynAlbum;
        final int yngroupid = ynAlbum.getGroupid();
        viewHold.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YNSingleton.getInstance().setCurAlbum(ynAlbum2);
                ((BaseApplication) context.getApplicationContext()).setCurGid(yngroupid);
                context.sendBroadcast(new Intent(ACTIONs.actionUpdateFristAlbum).putExtra("update", true));
                context.sendBroadcast(new Intent(ACTIONs.actionUpdateFristAlbum).putExtra("openpublish", true));
                context.sendBroadcast(new Intent(ACTIONs.actionToPeopleTimeLine));
                context.sendBroadcast(new Intent("noticefinish"));

            }
        });
        return convertView;
    }

    class ViewHold {
        private LinearLayout item;
        private SimpleDraweeView pic;
        private ImageView ok;
        private TextView name;
    }


}
